﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace Simple_PM.Services.Membership
{
    public class CustomMembershipProvider: SqlMembershipProvider
    {
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            MembershipUser membershipUser = null;
            using(var context = new SimplePmEntities())
            {
                if(IsUsernameAlreadyTaken(username))
                {
                    status = MembershipCreateStatus.DuplicateUserName;
                    return null;
                }

                if (IsEmailAddressAlreadyRegistered(email))
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                    return null;
                }

                context.Users.AddObject(new User
                                            {
                                                UserName = username,
                                                Password = password,
                                                EmailAddress = email
                                            });

                try
                {
                    context.SaveChanges();
                    status = MembershipCreateStatus.Success;
                    return null;
                }
                catch
                {   
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            return membershipUser;
        }

        public override bool ValidateUser(string username, string password)
        {
            bool isUserValid = false;

            using(var context = new SimplePmEntities())
            {
                //var users = context.Users.ToList();

                //string msg = string.Empty;

                //foreach(var item in users)
                //{
                //    msg += "Username : " + item.UserName + " Password: " + item.Password + " : Email : " +
                //           item.EmailAddress;
                //    msg += Environment.NewLine;
                //}

                var user = context.Users.Where(m => m.UserName == username && m.Password == password).FirstOrDefault();
                if(user != null)
                {
                    isUserValid = true;
                }
            }

            return isUserValid;
        }

        private bool IsUsernameAlreadyTaken(string username)
        {
            bool isUsernameAlreadyTaken = false;
            using(var context = new SimplePmEntities())
            {
                var user = context.Users.Where(m => m.UserName == username).FirstOrDefault();
                if(user != null)
                {
                    isUsernameAlreadyTaken = true;
                }
            }

            return isUsernameAlreadyTaken;
        }

        private bool IsEmailAddressAlreadyRegistered(string email)
        {
            bool isEmailAddressAlreadyRegistered = false;
            using (var context = new SimplePmEntities())
            {
                var user = context.Users.Where(m => m.EmailAddress == email).FirstOrDefault();
                if (user != null)
                {
                    isEmailAddressAlreadyRegistered = true;
                }
            }

            return isEmailAddressAlreadyRegistered;
        }
    }
}